﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using SmartXLS;
using System.Drawing;
using UygulamaTakbisService.wsTakbisServis;

namespace UygulamaTakbisService
{
    public class RehinYaz
    {
        public byte[] Yaz(List<RehinBilgi> rehinList, string serverKayitYolu)
        {
            WorkBook workBook = new WorkBook();
            workBook.setSheetName(0, "Rehin Listesi");

            int satir = 0;
            int sutun = 0;

            workBook.setText(satir, sutun, "Rehin Miktar");
            sutun++;
            workBook.setText(satir, sutun, "Kredi Miktar");
            sutun++;
            workBook.setText(satir, sutun, "Vade");
            sutun++;
            workBook.setText(satir, sutun, "Para Birimi");
            sutun++;
            workBook.setText(satir, sutun, "Faiz");
            sutun++;
            workBook.setText(satir, sutun, "Rehin Tip");
            sutun++;
            workBook.setText(satir, sutun, "Kredi Türü");
            sutun++;
            workBook.setText(satir, sutun, "MuteselsilMi");
            sutun++;
            workBook.setText(satir, sutun, "Aktif Mi");
            sutun++;
            workBook.setText(satir, sutun, "Şartlar");
            sutun++;
            workBook.setText(satir, sutun, "Açıklama");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Yevmiye Kurum");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Yevmiye Kurum");
            sutun++;

            RangeStyle rangeStyleBaslik = workBook.getRangeStyle(0, 0, 0, sutun);
            rangeStyleBaslik.FontBold = true;
            rangeStyleBaslik.FontColor = Color.DarkRed.ToArgb();
            rangeStyleBaslik.HorizontalAlignment = RangeStyle.HorizontalAlignmentCenter;
            rangeStyleBaslik.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
            rangeStyleBaslik.LeftBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.RightBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.TopBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.BottomBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.VerticalInsideBorder = RangeStyle.BorderThin;
            workBook.setRangeStyle(rangeStyleBaslik, 0, 0, 0, sutun - 1);

            string deger = string.Empty;
            foreach (RehinBilgi rehin in rehinList)
            {
                satir++;
                sutun = 0;
                if (rehin.RehinMiktar != null)
                    deger = rehin.RehinMiktar.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.KrediMiktar != null)
                    deger = rehin.KrediMiktar.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.Vade != null)
                    deger = rehin.Vade.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.Doviz != null)
                {
                    if (rehin.Doviz.Ad != null)
                        deger = rehin.Doviz.Ad.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.Faiz != null)
                    deger = rehin.Faiz.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.KrediTur != null)
                    deger = rehin.KrediTur.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.MuteselsilMi != null)
                    deger = rehin.MuteselsilMi.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TapuBolumDurum != null)
                    deger = rehin.TapuBolumDurum.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.Sartlar != null)
                    deger = rehin.Sartlar.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.Aciklama != null)
                    deger = rehin.Aciklama.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TesisIslem != null)
                {
                    if (rehin.TesisIslem.IslemTanimAd != null)
                        deger = rehin.TesisIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TesisIslem != null && rehin.TesisIslem.Yevmiye != null)
                {
                    if (rehin.TesisIslem.Yevmiye.YevmiyeNo != null)
                        deger = rehin.TesisIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TesisIslem != null && rehin.TesisIslem.Yevmiye != null)
                {
                    if (rehin.TesisIslem.Yevmiye.Tarih != null)
                        deger = rehin.TesisIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TesisIslem != null && rehin.TesisIslem.Yevmiye != null)
                {
                    if (rehin.TesisIslem.Yevmiye.Kurum != null)
                        deger = rehin.TesisIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TerkinIslem != null)
                {
                    if (rehin.TerkinIslem.IslemTanimAd != null)
                        deger = rehin.TerkinIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TerkinIslem != null && rehin.TerkinIslem.Yevmiye != null)
                {
                    if (rehin.TerkinIslem.Yevmiye.YevmiyeNo != null)
                        deger = rehin.TerkinIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TerkinIslem != null && rehin.TerkinIslem.Yevmiye != null)
                {
                    if (rehin.TerkinIslem.Yevmiye.Tarih != null)
                        deger = rehin.TerkinIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (rehin.TerkinIslem != null && rehin.TerkinIslem.Yevmiye != null)
                {
                    if (rehin.TerkinIslem.Yevmiye.Kurum != null)
                        deger = rehin.TerkinIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                RangeStyle rangeStyleVeri = workBook.getRangeStyle(satir, 0, satir, sutun);
                rangeStyleVeri.HorizontalAlignment = RangeStyle.HorizontalAlignmentLeft;
                rangeStyleVeri.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
                rangeStyleVeri.LeftBorder = RangeStyle.BorderThin;
                rangeStyleVeri.RightBorder = RangeStyle.BorderThin;
                rangeStyleVeri.TopBorder = RangeStyle.BorderThin;
                rangeStyleVeri.BottomBorder = RangeStyle.BorderThin;
                rangeStyleVeri.VerticalInsideBorder = RangeStyle.BorderThin;
                workBook.setRangeStyle(rangeStyleVeri, satir, 0, satir, sutun);
            }

            for (int i = 0; i < sutun + 1; i++)
                workBook.setColWidthAutoSize(i, true);

            workBook.write(serverKayitYolu);
            workBook.Dispose();

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            return File.ReadAllBytes(serverKayitYolu);
        }
    }
}

